/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing.Design;

namespace DaveSexton.DocProject.Sandcastle.BuildComponents
{
	internal sealed class SandcastleBuildComponentPropertyDescriptor : PropertyDescriptor
	{
		#region Public Properties
		public override bool IsReadOnly
		{
			get
			{
				return false;
			}
		}

		public override Type ComponentType
		{
			get
			{
				return typeof(SandcastleBuildComponentCollection);
			}
		}

		public int ComponentIndex
		{
			get
			{
				return buildComponent.Index;
			}
		}

		public override Type PropertyType
		{
			get
			{
				return typeof(object);
			}
		}
		#endregion

		#region Private / Protected
		private readonly SandcastleBuildComponentManager manager;
		private readonly SandcastleBuildComponent buildComponent;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="SandcastleBuildComponentPropertyDescriptor" /> class.
		/// </summary>
		public SandcastleBuildComponentPropertyDescriptor(SandcastleBuildComponentManager manager, SandcastleBuildComponent buildComponent)
			: base(buildComponent.UniqueName, buildComponent.Attributes.AttributesArray)
		{
			if (manager == null)
				throw new ArgumentNullException("manager");

			this.manager = manager;
			this.buildComponent = buildComponent;
		}
		#endregion

		#region Methods
		public override bool CanResetValue(object component)
		{
			return false;
		}

		public override object GetValue(object component)
		{
			return buildComponent;
		}

		public override void ResetValue(object component)
		{
			throw new NotSupportedException();
		}

		public override void SetValue(object component, object value)
		{
			if (value is bool)
			// Component editors are expected to return true if changes made to the XML configuration must be saved
			{
				bool changed = (bool) value;

				if (changed)
				{
					buildComponent.CommitChanges();
					manager.NotifyChanged(buildComponent, true);
				}
				else
					buildComponent.CancelChanges();
			}
		}

		public override bool ShouldSerializeValue(object component)
		{
			return false;
		}

		public override string ToString()
		{
			return (buildComponent.IsDirty) ? "Modified" : "";
		}
		#endregion
	}
}
